package com.server.userCenter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.config.AppReturn;
import com.server.user.AppUserBaseCtrl;
import com.server.wx.WxUtil;
import com.util.IOUtil;
import com.util.StrKit;

/**
 * 用户中心
 * @author dell
 *
 */
@Controller
@RequestMapping("/appUser/userCenter")
public class UserCenterCtrl extends AppUserBaseCtrl{

	@Autowired
	UserCenterService ucService;
	@Autowired
	RewardUtil rewardUtil;
	@Autowired
	WxUtil wxUtil;
	

	//跳转到客服中心
	@RequestMapping("/goKf")
	public ModelAndView goKf()
	{
		ModelAndView mav = new ModelAndView("userCenter/kf");
		return mav;
	}
	
	//跳转到预约记录页面
	@RequestMapping("/goAppointPage")
	public ModelAndView goAppointPage()
	{
		ModelAndView mav = new ModelAndView("userCenter/appointList");
		return mav;
	}
	
	//地址管理页面
	@RequestMapping("/goAddrPage")
	public ModelAndView goAddrPage(@RequestParam(name="ids",required=false)String ids)
	{
		ModelAndView mav = new ModelAndView("userCenter/addr");
		if(StrKit.notBlank(ids)){
			mav.addObject("ids", ids);
		}
		return mav;
	}
	
	//跳转到用户中心首页
	@RequestMapping("/goUCIndex")
	public ModelAndView goUCIndex()
	{
		ModelAndView mav = new ModelAndView("userCenter/index");
		refreshUser();
		mav.addObject("user", getLogUser());
		
//		mav.addObject("data", ucService.getMyUcData(getLogUser()));
		
		return mav;
	}
	
	//跳转到用户资料
	@RequestMapping("/goUCInfo")
	public ModelAndView goUCInfo()
	{
		ModelAndView mav = new ModelAndView("userCenter/info");
		mav.addObject("user", getLogUser());
		return mav;
	}
	
	//跳转到我的订单
	@RequestMapping("/goUCList")
	public ModelAndView goUCList()
	{
		ModelAndView mav = new ModelAndView("userCenter/list");
		
		return mav;
	}	
	
	//跳转到礼券页面
	@RequestMapping("/goLQ")
	public ModelAndView goLQ()
	{
		ModelAndView mav = new ModelAndView("userCenter/lq");
		refreshUser();
		mav.addObject("user", getLogUser());
		//礼券使用量
		int lqnum = ucService.countUserLQ(getLogUser());
		mav.addObject("lqnum", lqnum);
		return mav;
	}	
	
	//跳转到礼券记录页面
	@RequestMapping("/goLQList")
	public ModelAndView goLQList()
	{
		ModelAndView mav = new ModelAndView("userCenter/lqList");
		return mav;
	}		
		
	//跳转到爱豆页面
	@RequestMapping("/goAD")
	public ModelAndView goAD()
	{
		ModelAndView mav = new ModelAndView("userCenter/ad");
		refreshUser();
		mav.addObject("user", getLogUser());
		return mav;
	}	
	
	//跳转到爱豆记录页面
	@RequestMapping("/goADList")
	public ModelAndView goADList()
	{
		ModelAndView mav = new ModelAndView("userCenter/adList");
		return mav;
	}		
	//跳转到爱豆分红页面
	@RequestMapping("/goADAmount")
	public ModelAndView goADAmount()
	{
		ModelAndView mav = new ModelAndView("userCenter/adAmount");
		return mav;
	}					

	//跳转到我的钱包页面
	@RequestMapping("/goMyPackage")
	public ModelAndView goMyPackage()
	{
		refreshUser();
		ModelAndView mav = new ModelAndView("userCenter/mypackage");
		Map<String, Object> user = getLogUser();
		mav.addObject("user", user);
		//历史提现记录
		double txamount = ucService.countCashLog(getLogUser());
		mav.addObject("txamount", txamount);
		
		return mav;
	}		
	
	//查询交易记录
	@RequestMapping("/goAmountLog")
	public ModelAndView goAmountLog()
	{
		ModelAndView mav = new ModelAndView("userCenter/moneylog");
		
		List<Map<String, Object>> list=ucService.getAmountLog(getLogUser());
		mav.addObject("list", list);
		
		return mav;
	}
	//查询提现记录
	@RequestMapping("/goCashLog")
	public ModelAndView goCashLog()
	{
		ModelAndView mav = new ModelAndView("userCenter/cashlog");
		
		List<Map<String, Object>> list=ucService.getCashLog(getLogUser());
		mav.addObject("list", list);
		
		return mav;
	}
	/**
	 * 我的数据,直接跳转H5
	 * @return
	 */
	@RequestMapping("/myData")
	public ModelAndView myData()
	{
		ModelAndView mav = new ModelAndView("userCenter/myData");
		Map<String, Object> ucMap = ucService.getMyData(getLogUser());
		mav.addObject("ucMap", ucMap);
		
		return mav;
	}
	/**
	 * 关于我们,直接跳转H5
	 * @return
	 */
	@RequestMapping("/aboutUs")
	public ModelAndView aboutUs()
	{
		ModelAndView mav = new ModelAndView("userCenter/aboutUs");
		
		
		return mav;
	}
	/**
	 * 客服中心
	 * @return
	 */
	@RequestMapping("/help")
	public ModelAndView help()
	{
		ModelAndView mav = new ModelAndView("userCenter/help");
		
		
		return mav;
	}	
	
	/**
	 * 查询我的会员,直接跳转H5
	 * @param status
	 * @return
	 */
	@RequestMapping("/goMyMember")
	public ModelAndView goMyMember()
	{
		ModelAndView mav = new ModelAndView("userCenter/myMember");
		List<Map<String, Object>> data = ucService.myMember(getLogUser());
		mav.addObject("list", data);
		mav.addObject("num", data.size());
		return mav;
	}
	
	/**
	 * 查询我的二维码
	 * @param status
	 * @return
	 */
	@RequestMapping("/goMyCodePage")
	public ModelAndView goMyCodePage()
	{
		ModelAndView mav = new ModelAndView("userCenter/myCode");
		Map<String, Object> user = getLogUser();
		mav.addObject("detail", user);
		
		return mav;
	}	
	
	/**
	 * 查询我的卡金
	 * @param status
	 * @return
	 */
	@RequestMapping("/goMyCard")
	public ModelAndView goMyCard()
	{
		ModelAndView mav = new ModelAndView("userCenter/myCard");
		mav.addObject("detail", getLogUser());
		
		return mav;
	}	
	
	
	
	
	
	
	
	////////////////逻辑
	
	/**
	 * 小额提现_只支持200
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/doTx200")
	public String doTx200()
	{
		refreshUser();
		AppReturn ret = ucService.doTx(getLogUser(), "1");
		return ret.toString();
	}
	/**
	 * 普通提现,只支持500
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/doTx500")
	public String doTx500()
	{
		refreshUser();
		AppReturn ret = ucService.doTx(getLogUser(), "2");
		return ret.toString();
	}
	/**
	 * 发送验证码
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/sendCode")
	public String sendCode()
	{
		String code = IOUtil.getRandom(4);
		System.out.println("code : "+code);
		getSession().setAttribute("code", code);
		AppReturn ret = new AppReturn("00");
		return ret.toString();
		
	}

	/**
	 * 查询地址列表
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/myAddr")
	public String getAddr()
	{
		List<Map<String, Object>> list = ucService.getAddr(getLogUser());
		AppReturn ret = StrKit.list2Appreturn(list);
		return ret.toString();
	}
	
	/**
	 * 新增地址
	 * 如果ID为空，则为新增。如果ID不为空，则为修改
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/addAddr")
	public String addAddr(@RequestParam(name="id",required=false)String id,@RequestParam("province")String province,@RequestParam("city")String city,@RequestParam("area")String area,
			@RequestParam("addr")String addr,@RequestParam("phone")String phone,@RequestParam("name")String name,@RequestParam("isdefault")String isdefault)
	{
		AppReturn ret = null;
		if(StrKit.isBlank(id))
		{
			ret = ucService.addAddr(getLogUser(), province, city, area, addr, phone, name,isdefault);
		}else{
			ret = ucService.editAddr(getLogUser(), id, province, city, area, addr, phone, name,null, isdefault);
		}
		
		return ret.toString();
	}
	
	/**
	 * 编辑地址
	 * @param province
	 * @param city
	 * @param area
	 * @param addr
	 * @param phone
	 * @param name
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/editAddr")
	public String editAddr(@RequestParam("id")String id,@RequestParam(name="province",required=false)String province,@RequestParam(name="city",required=false)String city
			,@RequestParam(name="area",required=false)String area,@RequestParam(name="addr",required=false)String addr
			,@RequestParam(name="phone",required=false)String phone,@RequestParam(name="name",required=false)String name)
	{
		AppReturn ret = ucService.editAddr(getLogUser(), id, province, city, area, addr, phone, name, null, null);
		return ret.toString();
	}
	
	/**
	 * 删除收获地址
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delAddr")
	public String delAddr(@RequestParam("id")String id)
	{
		AppReturn ret = ucService.editAddr(getLogUser(), id, null, null, null, null, null, null, "2", null);
		return ret.toString();
	}
	
	/**
	 * 设置默认收获地址
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/setDefaultAddr")
	public String setDefaultAddr(@RequestParam("id")String id)
	{
		AppReturn ret = ucService.setDefaultAddr(getLogUser(), id);
		return ret.toString();
	}
	
	/**
	 * 获取用户默认地址，没有默认返回最后一个
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getDefaultAddr")
	public String getDefaultAddr()
	{
		AppReturn ret = ucService.getDefaultAddr(getLogUser());
		return ret.toString();
	}
	
	
	/**
	 * 查询我得订单
	 * @param status
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/myList")
	public String myList(@RequestParam(name="status",required=false)String status)
	{
		AppReturn ret = ucService.getList(getLogUser(), status);
		
		return ret.toString();
	}
	/**
	 * 查询我订单详情,
	 * @param status
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/myListDetail")
	public String myListDetail(@RequestParam(name="id")String id)
	{
		AppReturn ret = ucService.getListDetail(getLogUser(), id);
		
		return ret.toString();
	}
	/**
	 * 取消订单
	 * @param status
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/cancelList")
	public String cancelList(@RequestParam(name="id")String id)
	{
//		AppReturn ret = ucService.getListDetail(getLogUser(), id);
		
		AppReturn ret = ucService.cancelList(getLogUser(), id);
		return ret.toString();
	}
	/**
	 * 删除订单
	 * @param status
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delList")
	public String delList(@RequestParam(name="id")String id)
	{
//		AppReturn ret = ucService.getListDetail(getLogUser(), id);

		AppReturn ret = ucService.delList(getLogUser(), id);
		return ret.toString();
	}
	/**
	 * 获取礼券记录
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getLQList")
	public String getLQList()
	{
		List<Map<String, Object>> data = ucService.getLQLog(getLogUser(), getReqMap());
		AppReturn ret = StrKit.list2Appreturn(data);
		
		return ret.toString();
	}
	
	/**
	 * 查询艾豆明细
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getADList")
	public String getADList()
	{
		List<Map<String, Object>> data = ucService.getADList(getLogUser(), getReqMap());
		
		AppReturn ret = StrKit.list2Appreturn(data);
		
		return ret.toString();
	}
	/**
	 * 查询艾豆分红
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getADAmount")
	public String getADAmount()
	{
		List<Map<String, Object>> data = ucService.getADAmount(getLogUser(), getReqMap());
		
		AppReturn ret = StrKit.list2Appreturn(data);
		
		return ret.toString();
	}
	/**
	 * 余额支付
	 * @param listid 订单ID
	 * @param pwd   用户密码
	 * @param type 1:余额支付，2：购物金支付
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/payOffLine")
	public String payOffLine(@RequestParam("listid")String listid,@RequestParam("pwd")String pwd,@RequestParam(name="type",required=false,defaultValue="1")String type)
	{
		refreshUser();
		AppReturn ret = ucService.payOffLine(getLogUser(), listid,pwd,type);
		return ret.toString();
	}
	/**
	 * 实名用户
	 * @param status
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/realname")
	public String realname(@RequestParam(name="realname")String realname,@RequestParam(name="cardcode")String cardcode)
	{
		AppReturn ret = ucService.realname(getLogUser(), realname, cardcode);
		return ret.toString();
	}
	
	/**
	 * 根据推荐码、或手机号查询用户，精准查询
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getUserByCP")
	public String getUserByCP(@RequestParam(name="serchStr")String serchStr)
	{
		AppReturn ret = ucService.getUserByCP(serchStr);
		return ret.toString();
	}
	
	/**
	 * 转账
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/transferAmount")
	public String transferAmount(@RequestParam("reciveId")String reciveId,@RequestParam("amount")String amount,
			@RequestParam("pwd")String pwd)
	{
		AppReturn ret = ucService.transferAmount(getLogUser(), reciveId, amount, pwd);
		return ret.toString();
	}
	
	/**
	 * 修改密码
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/changePWD")
	public String changePWD(@RequestParam("oldPwd")String oldPwd,@RequestParam("newPwd")String newPwd)
	{
		AppReturn ret = ucService.changePWD(getLogUser(), oldPwd, newPwd);
		return ret.toString();
	}
	/**
	 * 修改支付密码
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/changePWD_pay")
	public String changePWD_pay(@RequestParam("oldPwd")String oldPwd,@RequestParam("newPwd")String newPwd)
	{
		AppReturn ret = ucService.changePWD_pay(getLogUser(), oldPwd, newPwd);
		return ret.toString();
	}
	/**
	 * 修改用户信息：名称、头像
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/editUser")
	public String editUser(@RequestParam(name="realName",required=false)String realName,@RequestParam(name="head",required=false)String head,@RequestParam(name="phone",required=false)String phone)
	{
		//校验验证码
//		if(getSession().getAttribute("code")==null||!getSession().getAttribute("code").equals(code))
//		{
//			AppReturn ret = new AppReturn("09", "验证码错误！");
//			return ret.toString();
//		}
		
		AppReturn ret = ucService.editUser(getLogUser(), realName, head,phone);
		refreshUser();
		return ret.toString();
	}
	/**
	 * 查询加盟记录
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getShopJoinLog")
	public String getShopJoinLog()
	{
		AppReturn ret = ucService.getShopJoinLog(getLogUser());
		return ret.toString();
	}

	/**
	 * 取消加盟
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/cancelShopJoin")
	public String cancelShopJoin(@RequestParam("id")String id)
	{
		AppReturn ret = ucService.cancelShopJoin(getLogUser(), Integer.parseInt(id));
		return ret.toString();
	}
	
	/**
	 * 退出登录
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/goLogOff")
	public String goLogOff()
	{
		AppReturn ret = ucService.goLogOff(getLogUser());
		//清空session
		getSession().setAttribute("wxuser", null);
		
		return ret.toString();
	}
}
